package com.sprucetec.live.config;

import com.sprucetec.live.common.exceptioin.SprucetecException;
import com.sprucetec.live.common.util.RespCode;
import com.sprucetec.live.common.util.ServiceResult;
import com.sprucetec.live.common.util.ServiceResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created with IntelliJ IDEA.
 * User: zhangyb
 * Date: 2018/11/15
 * Time: 15:52
 * Description: 全局异常处理
 **/
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理系统级未catch异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = RuntimeException.class)
    @ResponseBody
    public ServiceResult<String> handleSystemException(RuntimeException e) {
        log.error("系统异常", e);
        return ServiceResultUtil.fail(RespCode.ERROR, "系统异常，请稍后重试");
    }

    /**
     * 处理业务级手动throw的异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = SprucetecException.class)
    @ResponseBody
    public ServiceResult<String> handleAssetException(SprucetecException e) {
        return ServiceResultUtil.fail(RespCode.ERROR, e.getMessage());
    }
}
